﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Snake {
    public class HitSnake : MonoBehaviour {
        [SerializeField]
        private CircleCollider2D snakeHeaderCircleCollider2D;
        [SerializeField]
        private FoodManager foodManager;
        [SerializeField]
        private SnakeManager snakeManager;

        private ContactFilter2D filter;
        private Collider2D[] listCollider2D = new Collider2D[5];

        void Start() {
            filter = filter.NoFilter();
        }

        void Update() {
            int count = snakeHeaderCircleCollider2D.OverlapCollider(filter, listCollider2D);
            if (count > 0) {
                for (int i = 0; i < count; ++i) {
                    Collider2D de = listCollider2D[i];
                    //Debug.Log(de.name);
                    if (de.name.StartsWith("item")) {
                        OnSnakeHit(de);
                    }
                }
            }
        }

        private void OnSnakeHit(Collider2D collider2D) {
            Food food = collider2D.gameObject.GetComponent<Food>();
            if (null == food)
                return;

            snakeManager.getGameSnake().Eat(food);
            foodManager.RemoveSweet(food.transform);
        }
    }
}